package hutech.ksv.dao;

import hutech.ksv.entity.ThongTinTuyenDungEntity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TTTuyenDungDao implements Serializable {

	@Autowired
	DataSource dataSource;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Test connection
	public Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/mydb", "root", "mysql@123");
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void insertTTTuyenDung(ThongTinTuyenDungEntity ttTD) {
		String query = "INSERT INTO `ThongTinTuyenDung` (`NgayLapTTTD`, `TongSoLuongTD`, `NgayBatDauTD`, `NgayKetThucTD`, `TrangThaiTTTD`, `TrangThaiDuyetTTTD`, `TenTKCT`, `TenTKQTV`) VALUES (?,?,?,?,?,?,?,?)";
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		try {
			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);

			preparedStatement.setDate(1, ttTD.getNgayLapTTTD());
			preparedStatement.setInt(2, ttTD.getTongSoLuongTD());
			preparedStatement.setDate(3, ttTD.getNgayBatDauTD());
			preparedStatement.setDate(4, ttTD.getNgayKetThucTD());
			preparedStatement.setBoolean(5, ttTD.isTrangThaiTTTD());
			preparedStatement.setBoolean(6, ttTD.isTrangThaiDuyetTTTD());
			preparedStatement.setString(7, ttTD.getTenTKCT());
			preparedStatement.setString(8, ttTD.getTenTKQTV());

			preparedStatement.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void updateTTTuyenDung(ThongTinTuyenDungEntity ttTD) {
		String query = "UPDATE `ThongTinTuyenDung` "
				+ "SET `NgayLapTTTD`=?, `TongSoLuongTD`=?, `NgayBatDauTD`=?,"
				+ "`NgayKetThucTD`=?, `TrangThaiTTTD`=?, `TrangThaiDuyetTTTD`=?,"
				+ "`TenTKQTV`=?, `TenTKQTV`=?" + "WHERE `MaTTTD`=?";
		Connection conn = null;
		PreparedStatement preparedStatement = null;

		try {
			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);

			preparedStatement.setDate(1, ttTD.getNgayLapTTTD());
			preparedStatement.setInt(2, ttTD.getTongSoLuongTD());
			preparedStatement.setDate(3, ttTD.getNgayBatDauTD());
			preparedStatement.setDate(4, ttTD.getNgayKetThucTD());
			preparedStatement.setBoolean(5, ttTD.isTrangThaiTTTD());
			preparedStatement.setBoolean(6, ttTD.isTrangThaiDuyetTTTD());
			preparedStatement.setString(7, ttTD.getTenTKCT());
			preparedStatement.setString(8, ttTD.getTenTKQTV());
			preparedStatement.setInt(9, ttTD.getMaTTTD());

			preparedStatement.execute();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// Load thong tin nha tro
	public List<ThongTinTuyenDungEntity> loadThongTinTD() {
		String query = "SELECT * FROM `ThongTinTuyenDung`";
		List<ThongTinTuyenDungEntity> listThongTinTD = new ArrayList<ThongTinTuyenDungEntity>();

		Connection conn = null;
		PreparedStatement preparedStatement = null;

		try {
			conn = dataSource.getConnection();
			preparedStatement = conn.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				listThongTinTD.add(new ThongTinTuyenDungEntity(rs.getInt(1), rs
						.getDate(2), rs.getInt(3), rs.getDate(4), rs
						.getDate(5), rs.getBoolean(6), rs.getBoolean(7), rs.getString(8), rs
						.getString(9)));
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listThongTinTD;
	}
}
